/*
 * @Descripttion: 后台管理系统
 * @version: 1.0
 * @Author: 狸猫
 * @Date: 2024-04-03 14:40:11
 * @LastEditors: 赛罗
 * @LastEditTime: 2024-05-20 14:15:36
 */
export const getDayOrWeek = (date) => {
  if (date == null) return "";
  date = new Date(date);
  const dayArr = ["前天", "昨天", "今天", "明天", "后天"];
  // 星期数组
  const weekArr = ["日", "一", "二", "三", "四", "五", "六"];

  // 获取传入日期与当前日期相差的天数，一天 86400000 毫秒
  const diffTime =
    (date.setHours(0, 0, 0, 0) - new Date().setHours(0, 0, 0, 0)) / 86400000;
  // console.log("diff", diffTime, typeof diffTime);
  // 获取“前天/昨天/今天/明天/后天”，若不在这些里面则显示星期
  // return `周${weekArr[date.getDay()]}`;
  return diffTime === 0 ? dayArr[diffTime + 2] : `周${weekArr[date.getDay()]}`;
};
export const getKeyWeek = (date) => {
  if (date == null) return "";
  date = new Date(date);
  const dayArr = ["前天", "昨天", "今天", "明天", "后天"];
  // 星期数组
  const weekArr = ["日", "一", "二", "三", "四", "五", "六"];

  // 获取传入日期与当前日期相差的天数，一天 86400000 毫秒
  const diffTime =
    (date.setHours(0, 0, 0, 0) - new Date().setHours(0, 0, 0, 0)) / 86400000;
  // console.log("diff", diffTime, typeof diffTime);
  // 获取“前天/昨天/今天/明天/后天”，若不在这些里面则显示星期
  // return `周${weekArr[date.getDay()]}`;
  return date.getDay() == 0 ? 7 : date.getDay();
};
export const formatDate = (value, fmt) => {
  var date = new Date(value);
  var o = {
    "M+": date.getMonth() + 1, //月份
    "d+": date.getDate(), //日
    "h+": date.getHours(), //小时
    "m+": date.getMinutes(), //分
    "s+": date.getSeconds(), //秒
    "w+": date.getDay(), //星期
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
    S: date.getMilliseconds(), //毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  for (var k in o) {
    if (k === "w+") {
      if (o[k] === 0) {
        fmt = fmt.replace("w", "周日");
      } else if (o[k] === 1) {
        fmt = fmt.replace("w", "周一");
      } else if (o[k] === 2) {
        fmt = fmt.replace("w", "周二");
      } else if (o[k] === 3) {
        fmt = fmt.replace("w", "周三");
      } else if (o[k] === 4) {
        fmt = fmt.replace("w", "周四");
      } else if (o[k] === 5) {
        fmt = fmt.replace("w", "周五");
      } else if (o[k] === 6) {
        fmt = fmt.replace("w", "周六");
      }
    } else if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
    }
  }
  return fmt;
};
export const getNextDate = (date, day) => {
  var dd = date ? new Date(date) : new Date();
  dd.setDate(dd.getDate() + day);
  var y = dd.getFullYear();
  var m =
    dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
  var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
  return y + "-" + m + "-" + d;
};

export const inTime = (date, startTime, endTime) => {
  var curDate = new Date(date).getTime(),
    beginDate = new Date(startTime).getTime(),
    endDate = new Date(endTime).getTime() - 1000 * 60 * 60 * 24;
  //
  if(startTime == endTime){
    endDate = new Date(endTime).getTime()
  }
  //
  if (curDate >= beginDate && curDate <= endDate) {
    return true;
  }
  return false;
};

// 计算连个时间格式间相差多少天
export const DateDiff = (sDate1, sDate2) => { //sDate1和sDate2是2002-12-18格式

	let date1 = (sDate1 + '').replace(/-/g, '/')
	let date2 = (sDate2 + '').replace(/-/g, '/')
	const sDate = Date.parse(date1)
	const eDate = Date.parse(date2)
	if (sDate > eDate) {
		return 0
	}
	// 这个判断可以根据需求来确定是否需要加上
	if (sDate === eDate) {
		return 1
	}
	const days = (eDate - sDate) / (1 * 24 * 60 * 60 * 1000)
	return days

}
